// pages/timeConsumption/timeConsumption.js
var util = require('../../utils/util');
const App = getApp()

Page({
  data: {
    date: `${new Date().getFullYear()}-${new Date().getMonth() + 1>9?new Date().getMonth() + 1:'0'+(new Date().getMonth() + 1)}`, //日期
    currentItem: 1,
    detailList: [],
    detailObj: {},
    total: 0,
    params: {
      pageNum: 1,
      pageSize: 10
    }
  },
  onLoad(options) {
    this.getDetailData()
  },
  bindDateChange(e) {
    this.setData({
      date: e.detail.value,
      detailList: [],
      detailObj: {},
      params: {
        pageNum: 1,
        pageSize: 10
      }
    })
    this.getDetailData()
  },
  changeType(e) {
    // 切换课程类型
    let item = e.currentTarget.dataset.item
    if (item == this.data.currentItem) return
    this.setData({
      currentItem: item,
      detailList: [],
      detailObj: {},
      params: {
        pageNum: 1,
        pageSize: 10
      }
    })
    this.getDetailData()
  },
  getDetailData(e) {
    //  课程消耗详情 
    if (e && e.currentTarget.dataset.more) {
      this.setData({
        'params.pageNum': this.data.params.pageNum + 1
      })
    }
    if (this.data.currentItem == 1) {
      this.getBandCourse()
    } else {
      this.getVipCourse()
    }
  },
  async getBandCourse() {
    // 获取团课消耗
    let data = await App.$axios.request({
      url: "/boss/overview/consumptionBig",
      method: 'GET',
      loading: true,
      data: {
        queryMonth: this.data.date,
        ...this.data.params
      }
    })
    if (data) {
      this.setData({
        detailObj: data,
        detailList: [...this.data.detailList, ...data.rows],
        total: data.total
      })
    }
  },
  async getVipCourse() {
    // 获取VIP课消耗
    let data = await App.$axios.request({
      url: "/boss/overview/consumptionVip",
      method: 'GET',
      loading: true,
      data: {
        queryMonth: this.data.date,
        ...this.data.params
      }
    })
    if (data) {
      this.setData({
        detailObj: data,
        detailList: [...this.data.detailList, ...data.rows],
        total: data.total
      })
    }
  }
})